package com.falling.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.falling.reggie.common.BaseContext;
import com.falling.reggie.common.R;
import com.falling.reggie.entity.ShoppingCart;
import com.falling.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    private ShoppingCart queryAndJudge(ShoppingCart shoppingCart){
        // 设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        // 根据用户id查询
        queryWrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId());

        Long dishId = shoppingCart.getDishId();
        // 判断添加到购物车的是dish还是setmeal
        if (dishId != null)
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        else
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        return shoppingCartService.getOne(queryWrapper);

    }

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart cart = queryAndJudge(shoppingCart);
        // 如果查到，则添加的数量+1，没查到就新添加
        if (cart != null){
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
        } else {
            shoppingCartService.save(shoppingCart);
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 将结果统一到cart上，方便返回
            cart = shoppingCart;
        }

        return R.success(cart);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart cart = queryAndJudge(shoppingCart);

        if (cart != null){
            if (cart.getNumber() == 0) {
                shoppingCartService.removeById(cart.getId());
                return R.success(cart);
            }
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartService.updateById(cart);
        }
        return R.success(cart);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> cleanCart(){
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        if (shoppingCartService.remove(queryWrapper)) return R.success("delete success...");
        else return R.error("delete fail...");
    }







}
